<?php

class Application_Model_EcurieDAO
{
	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if(is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if(!$dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
 
	public function getDbTable()
	{
		if(null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Ecurie');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Ecurie $ecurie)
	{
		print_r($ecurie);
		$data = array(
			'ECUNUM' => $ecurie->getEcuNum(),
			'FPNUM' => $ecurie->getFpNum(),
			'ECUNOM' => $ecurie->getEcuNom(),
			'ECUNOMDIR' => $ecurie->getEcuNomDir(),
			'ECUADRSIEGE' => $ecurie->getEcuAdrSiege(),
			'ECUPOINTS' => $ecurie->getEcuPoints(),
			'PAYNUM' => $ecurie->getPays()->getPayNum(),
			'ECUADRESSEIMAGE' => $ecurie->getEcuAdresseImage()
		);
		if (null === ($ecuNum = $ecurie->getEcuNum()))
		{
			unset($data['ECUNUM']);
			$this->getDbTable()->insert($data);
        }
        else
        {
			$this->getDbTable()->update($data, array('ECUNUM = ?' => $ecuNum));
		}
		return true;
	}
	
	public function delete($id)
	{
		$piloteDAO = new Application_Model_PiloteDAO();
		$pilotes = $piloteDAO->findByEcurie($id);
		foreach($pilotes as $pilote)
		{
			$piloteDAO->delete($pilote->getPilNum());
		}
		$voitureDbTable = new Application_Model_DbTable_Voiture();
		$voitureDbTable->delete(array('ECUNUM = ?' => $id));
		$financeDbTable = new Application_Model_DbTable_Finance();
		$financeDbTable->delete(array('ECUNUM = ?' => $id));
		$this->getDbTable()->delete(array('ECUNUM = ?' => $id));
	}
	
	public function find($ecuNum)
	{
		$ecurie = new Application_Model_Ecurie();
		$paysDAO = new Application_Model_PaysDAO();
		$result = $this->getDbTable()->find($ecuNum);
		if(0 == count($result))
		{
			return null;
		}
		$row = $result->current();
		$ecurie->setEcuNum($row->ECUNUM)
			   ->setFpNum($row->FPNUM)
			   ->setEcuNom($row->ECUNOM)
			   ->setEcuNomDir($row->ECUNOMDIR)
			   ->setEcuAdrSiege($row->ECUADRSIEGE)
			   ->setEcuPoints($row->ECUPOINTS)
			   ->setEcuAdresseImage($row->ECUADRESSEIMAGE);
			   
		//On recupere le pays
		$pays = $paysDAO->find($row->PAYNUM);
		$ecurie->setPays($pays);
		
		return $ecurie;
	}
	
	public function findAll()
	{
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->from('ecurie',array('ECUNUM'))->order('ECUNOM ASC'));
		$entries = array();
		foreach($resultSet as $row)
		{
			$entries[] = $this->find((int)$row->ECUNUM);
		}
		return $entries;
	}
	
	public function calculerPoints($id)
	{
		$piloteDAO = new Application_Model_PiloteDAO();
		$points=0;
		$pilotes = $piloteDAO->findByEcurie($id);
		foreach($pilotes as $pilote)
		{
			$points=$points+$pilote->getPilPoints();
		}
		return $points;
	}
}